# include <stdio.h>


int findDiff1(int* a, int len, int* result)
{
    if(len<=0 | a == NULL)
        return -1;

    result[0] = 0;
    for (int i = 0; i < len ; i ++)
        result[0] ^= a[i];
    return 0;
}
/*
    Find their diff bit. and then classify the data based on the diffbit
*/
int findDiff2(int* a, int len, int* result)
{
    if(len<=0 | a == NULL)
        return -1;

    int xor_result = 0;
    for (int i = 0; i < len; i++)
        xor_result ^= a[i];
    int diffbit = (xor_result & (xor_result - 1)) ^ xor_result;
    result[0] = 0;
    result[1] = 0;
    for(int i = 0; i < len; i++)
        if (a[i] & diffbit)
            result[0] ^= a[i];
        else
            result[1] ^= a[i];
    return 0;
}
/*
Assume the diffdata as a, b, c.
x = a ^ b, y = b ^ c, z = c ^ a;
then the lowbit of the x, y, z is different.
class xor_result^a[i] based on the difflow_bit get x(assume)
xor_result ^ x = c
then call find2
*/
int findDiff3(int* a, int len, int* result)
{
    if(len<=0 | a == NULL)
        return -1;
    
    unsigned int xor_result = 0;
    unsigned int diffbit = 0;
    unsigned int diffbit_2 = 0;
    unsigned int x_result = 0;
    int status = 0;
    unsigned int b[len];
    unsigned int i = 0;
    for (i = 0; i < len; i++)
        xor_result ^= a[i];
    printf("xor_result %d\n", xor_result);
    //update the data set by xor_result ^ a[i]
    for (i = 0; i< len; i++)
    {
        b[i] = xor_result ^ a[i];
        diffbit ^= (b[i] & (b[i] -1) ) ^ b[i];
        printf("%d %d\n", b[i], a[i]);
     }   

    printf("3 diffbit %d\n", diffbit);
    for (i = 0; i< len; i++)
        if (diffbit & b[i])
            x_result ^= b[i];

    result[0] = x_result ^ xor_result;

    result[1] = 0;
    result[2] = 0;

    for (i = 0; i< len; i++)
        if(a[i] == result[0])
        {
            a[i] = a[len - 1];
            break;
        }    
        
    xor_result = xor_result ^ result[0];
    diffbit_2 = (xor_result & (xor_result -1))^ xor_result;

    for(i = 0; i < len - 1; i++)
        if (a[i] & diffbit_2)
            result[1] ^= a[i];
        else
            result[2] ^= a[i];
    return 0;
}

int main ()
{
    const unsigned int len = 7;
    int status = 0;
    int result[3];
    int test_array[len] = {1, 2, 7, 3, 5, 5, 7};

    printf("array len %d\n", len);
    printf("test_array show\n");
    for (int i = 0; i < len; ++i)
    {
        printf("%d ", test_array[i]);
    }
    printf("\n");

    status = findDiff3(test_array, len, result);
    printf("Diff number 3: %d %d %d\n", result[0], result[1], result[2]);
}

/*status = findDiff1(test_array, len, result);
    if (status == -1)
        printf("%s\n", "get diff number failed");
    else
        printf("Diff number is %d\n", result[0]);

    int test_array2[8] = {1, 2 ,2, 3, 4, 1, 4, 5};
    status = findDiff2(test_array2, 8, result);
    if (status == -1)
        printf("%s\n", "get diff number failed");
    else
        printf("Diff number is %d %d\n", result[0], result[1]);*/